/*
This script performs the analyses presented in section H.1 of the appendix.
It makes use of the "Decision-making in the European Union" (DEU) dataset (Thomson 2006, 2012).
The DEU data is contained (together with hand-coded proposal dates) in "DEU_data_classified.dta".
Please be aware that you need to set the working directory to the main folder for the replication files.
Please install the lean2 sheme.
*/

//Set working directory to main directory for replication files
//cd "YOUR MAIN DIRECTORY FOR THE REPLICATION FILES"


//////////////////////////
//Prepare and merge data
//////////////////////////

//Load DEU data with coded dates of the legislative proposals
use "./data/DEU_data_classified.dta", clear

//Save outcome for each issue
egen outcome_aux = mean(position) if government == "outcome", by(n_issue)
replace outcome_aux = . if outcome == 999
egen outcome = mean(outcome_aux), by(n_issue)
drop outcome_aux

//Get rid of non-governmental positions
drop if government == "reference"
drop if government == "outcome"
drop if government == "ep"
drop if government == "com"

//Get rid of observations before EU accession
drop if proposal_date <= date("01/05/2004", "DMY") & government == "ee"
drop if proposal_date <= date("01/05/2004", "DMY") & government == "lt"
drop if proposal_date <= date("01/05/2004", "DMY") & government == "lv"
drop if proposal_date <= date("01/05/2004", "DMY") & government == "hu"
drop if proposal_date <= date("01/05/2004", "DMY") & government == "cz"
drop if proposal_date <= date("01/05/2004", "DMY") & government == "pl"
drop if proposal_date <= date("01/05/2004", "DMY") & government == "sk"
drop if proposal_date <= date("01/05/2004", "DMY") & government == "si"
drop if proposal_date <= date("01/05/2004", "DMY") & government == "mt"
drop if proposal_date <= date("01/05/2004", "DMY") & government == "cy"
drop if proposal_date <= date("01/01/2007", "DMY") & government == "ro"
drop if proposal_date <= date("01/01/2007", "DMY") & government == "bu"

//Create encoded country variable
gen country = .
replace country = 1 if government == "be"
replace country = 2 if government == "bu"
replace country = 3 if government == "cz"
replace country = 4 if government == "dk"
replace country = 5 if government == "de"
replace country = 6 if government == "ee"
replace country = 7 if government == "ie"
replace country = 8 if government == "el"
replace country = 9 if government == "es"
replace country = 10 if government == "fr"
replace country = 12 if government == "it"
replace country = 13 if government == "cy"
replace country = 14 if government == "lv"
replace country = 15 if government == "lt"
replace country = 16 if government == "lu"
replace country = 17 if government == "hu"
replace country = 18 if government == "mt"
replace country = 19 if government == "nl"
replace country = 20 if government == "at"
replace country = 21 if government == "pl"
replace country = 22 if government == "pt"
replace country = 23 if government == "ro"
replace country = 24 if government == "si"
replace country = 25 if government == "sk"
replace country = 26 if government == "fi"
replace country = 27 if government == "se"
replace country = 28 if government == "uk"

//Merge party positions
gen date = proposal_date
sort country date
merge m:1 country date using "./data/parties_cmp2019_online_2.2.dta"
drop if _merge == 2
drop _merge

//Save data
save "./generated_data/DEU_data_merged.dta", replace

//Merge macro-economic data
import excel "./data/eu_budget_contributions_2016.xlsx", sheet("Data") firstrow clear
keep year government eu_cont_gdp
drop if government == ""
save "./generated_data/economic_data_merged.dta", replace

import excel "./data/eurostat_inflation_hicp_2019.xls", sheet("Ready to load") firstrow clear
merge 1:1 year government using "./generated_data/economic_data_merged.dta"
drop _merge
save "./generated_data/economic_data_merged.dta", replace

import excel "./data/eurostat_unemployment_rate_2019.xls", sheet("Ready to load") firstrow clear
merge 1:1 year government using "./generated_data/economic_data_merged.dta"
drop _merge
replace government = "bu" if government == "bg"
sort government year
replace unemployment = . if unemployment == -999 //this recodes missing values
replace inflation = . if unemployment == -999  //this recodes missing values
save "./generated_data/economic_data_merged.dta", replace

//Merge datasets and sace
merge 1:m year government using "./generated_data/DEU_data_merged.dta"
save "./generated_data/DEU_data_merged.dta", replace


/////////////////////
//Prepare analysis
/////////////////////

//Create success measure
gen distance = abs(outcome-position)

//Format date
format proposal_date %tdCCYY

//Generate dummy for Eurosceptic govs
gen eurosceptic_gov = .
replace eurosceptic_gov = 1 if gov_eu_cmp_static <= 0 & gov_eu_cmp_static != .
replace eurosceptic_gov = 0 if gov_eu_cmp_static > 0 & gov_eu_cmp_static != .


/////////////////////////////////////
//Run models of bargaining success
/////////////////////////////////////

//Models reported in Table H1 in the appendix
xtset country
eststo clear
eststo: xtreg distance gov_eu_cmp_static gov_lr_cmp_static eu_cont_gdp unemployment inflation, fe vce(cluster country) 
eststo: xtreg distance c.proposal_date##c.gov_eu_cmp_static gov_lr_cmp_static eu_cont_gdp unemployment inflation, fe vce(cluster country) 
esttab, b(3) noomitted label replace se booktabs alignment(D{.}{.}{-1}) title(Models of bargaining success in the Council\label{tab1}) star(* 0.05 ** 0.01)

//Produce Figure H1 in the appendix
sum proposal_date if e(sample) == 1, detail
margins, dydx(gov_eu_cmp_static) at(proposal_date = (13187 (180) 17721))
marginsplot, scheme(lean2) recast(line) recastci(rline) ylabel(,glcolor(none) glwidth(vvthin)) ciopts(lpattern(longdash)) title("") xtitle("{bf:Proposal date}") ytitle("{bf:Marginal effect of government EU position}" "{bf:on distance to bargaining outcome}", size(medium))
graph export "./figures_appendix/figure_h_1.eps", replace
